<?php
namespace Admin\Controller;
use Think\Controller;
class DoctorController extends IndexController {

    public function lists(){
        $type= I('request.type');
        //type==1 执行添加医生的信息
        if($type==1){
            $doctor=D("Doctor");
            $data['logname']=I('post.logname');
            $data['name']=I("post.name");
            $data['hospital_id']=I("post.hospital_id");
           if(I("post.title_en")!=""){
           		 $title_en=I("post.title_en");
           		 $data['title_en']=implode(',',$title_en);
           }
           if(I("post.title_cn")!=""){

            $title_cn=I("post.title_cn");
            $data['title_cn']=implode(',',$title_cn);

           }
           if(I('post.speciality_en')!=""){
           	 $speciality_en=I('post.speciality_en');
             $data['speciality_en']=implode(',',$speciality_en);
           } 
           if(I('post.speciality_cn')!=""){
           		$speciality_cn=I('post.speciality_cn');
            	$data['speciality_cn']=implode(',',$speciality_cn);
           }
            
            $data['auth_status']=I('post.auth_status');
            $data['document_number']=I('post.document_number');
            $data['dept']=I("post.dept");
            $data['abstract']=I("post.abstract");
            $data['descrip']=I("post.descrip");
            $data['telephone']=I("post.telephone");
            $data['sex']=I("post.sex");
            $data['birthday']=I("post.birthday");
            $data['auth_status']=1;//认证中
            if($_FILES['img']['name']!=null){
                $upload = new \Think\Upload(); // 实例化上传类
                $upload->maxSize = 1024*1024 ;// 设置附件上传大小 (-1) 是不限值大小
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath="./imagecn_doc/";//设置上传根目录
                $upload->replace = true; //存在同名文件是否是覆盖
                $upload->autoSub = true;   // 是否使用子目录保存上传文件
                $upload->saveName = time().'_'.mt_rand();   // 采用date函数生成命名规则 传入Y-m-d参数
                $info   =   $upload->uploadOne($_FILES['img']);
                if(!$info){
                    $data['img']='';
                }else{
                    $data['img']=  ltrim($info['savepath'].$info['savename'],'/');
                }
            }
            if($_FILES['auth_img']['name']!=null){
                $upload = new \Think\Upload(); // 实例化上传类
                $upload->maxSize = 1024*1024 ;// 设置附件上传大小 (-1) 是不限值大小
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath="./imagecn_doc/";//设置上传根目录
                $upload->replace = true; //存在同名文件是否是覆盖
                $upload->autoSub = true;  // 是否使用子目录保存上传文件
                $upload->saveName = time().'_'.mt_rand();   // 采用date函数生成命名规则 传入Y-m-d参数
                $info   =   $upload->uploadOne($_FILES['auth_img']);
                if(!$info){
                    $data['auth_img']='';
                }else{
                    $data['auth_img']=  ltrim($info['savepath'].$info['savename'],'/');
                }
            }
            $res=$doctor->add($data);
            $this->redirect("Doctor/lists");
            $log=D("Log");
            $opt['name']=$_SESSION['name'];
            $opt['action']='add';
            $opt['model']='Doctor';
            $opt['create_time']=time();
            $opt['log']='员工号为'.$_SESSION['name'].'对医生模块执行了增加操作,增加了id为'.$res;
            $log->add($opt);
        }elseif($type==2){
            $this->actionUpdate();
        }
        $this->display();
    }


    public function get_lists(){
        $doctor=D("Doctor");
        $hospital=D("Hospital");

        if(I("request.iDisplayStart")==''){

            $start=0;
        }else{
            $start=I("request.iDisplayStart");
        }
        if(I("request.sSearch")!=''){
            $where['name'] = array('like','%'.I("request.sSearch").'%');;
        }else{
            $where='';
        }
        $res=$doctor
            ->field("id,name,logname,sex,abstract,descrip,title_en,title_cn,speciality_cn,speciality_en,auth_status,auth_img,hospital_id,document_number")
            ->where($where)
            ->limit($start,I("request.iDisplayLength"))
            ->select();
        if(!$res){
            echo json_encode(0);exit;
        }
        foreach($res as &$v){
            if($v['sex']==0){
                $v['sex']="女";
            }else{
                $v['sex']="男";
            }
            if($v['hospital_id']!=null){
                $r=$hospital->where('id='.$v['hospital_id'])->find();
                $v['hospital_id']=$r['name'];
            }
        }

        $ret = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $start,
            "iTotalDisplayRecords" => $doctor->count(),
            "aaData" => $res
        );

        echo json_encode($ret);exit;
    }
    public function add(){
        $this->display();
    }
    //获取国家列表
    public function country_list(){
        $Country=D("Country");
        $r=$Country->select();
        if(!$r){
            echo json_encode(0);exit;
        }
        echo json_encode($r);exit;
    }
    //获取列表
    public function city_list(){
        $cid=I("request.cid");
        $State=D("State");
        $r=$State->where('cid='.$cid)->select();
        if(!$r){
            echo json_encode(0);exit;
        }
        echo json_encode($r);exit;
    }
    //获取医院列表
    public function hospital_list(){
        $zid=I("request.zid");
        $hospital=D("Hospital");
        $result=$hospital->where('zid='.$zid)->select();

        if(!$result){
            echo json_encode(0);
        }
        echo json_encode($result);
    }


    //修改医生
    public function update(){
        $id=I("request.id");
        setcookie("uid", $id, time()+3600);
        $this->display();
    }
    public function update_list(){
        $id=$_COOKIE["uid"];
        $hos=D("Doctor");
        $r=$hos->where('id='.$id)->field('name,logname,hospital_id,abstract,descrip,telephone,speciality_en,speciality_cn,title_en,title_cn,dept,img')->find();
        if(!$r){
            echo json_encode(0);exit;
        }

        echo json_encode($r);exit;
    }
    //数据修改时呈现的医院信息
    public function get_hospital_one(){
        $hid=I("request.hid");
        $hospital= D('hospital');
        $result= $hospital->where("id=".$hid)->field("id,name,zid")->find();
        if(!$result){
            echo json_encode(0);exit;
        }
        echo json_encode($result);exit;

    }
    //数据修改时呈现的地区信息
    public function get_city_one(){
        $state=D('state');
        $cid=I("request.zid");
        $result= $state->where('id='.$cid)->field("id,name,cid")->find();
        if(!$result){
            echo json_encode(0);exit;
        }
        echo json_encode($result);exit;

    }
    //数据修改时获得国家信息
    public function get_country(){
        $country=D('country');
        $result= $country->select();
        if(!$result){
            echo json_encode(0);exit;
        }
        echo json_encode($result);exit;
    }

    // 更新数据
    public function actionUpdate(){
        $doctor=D('Doctor');
        $data['name']=I("post.name");
        $data['hospital_id']=I("post.hospital_id");
        $data['abstract']=I("post.abstract");
        $data['descrip']=I("post.descrip");
        $data['speciality_en']=I("speciality_en");
        $data['speciality_cn']=I("speciality_cn");
        $data['title_en']=I("post.title_en");
        $data['title_cn']=I("post.title_cn");
        $data['dept']=I("post.dept");
        $data['telephone']=I("post.telephone");
        if($_FILES['img']['name']!=null){
            if(isset($_COOKIE["uid"])){
                $r=$doctor->where('id='.$_COOKIE["uid"])->find();
                if($r['img']!=null){
                    unlink("./imagecn_doc/".$r['img']);
                }
            }
            header("content-type:text/html;charset=utf-8");
            $upload = new \Think\Upload(); // 实例化上传类
            $upload->maxSize = 1024*1024 ;// 设置附件上传大小 (-1) 是不限值大小
            $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath="./imagecn_doc/";
            $upload->replace = true; //存在同名文件是否是覆盖
            $upload->autoSub = true;  // 是否使用子目录保存上传文件
            $upload->saveName = time().'_'.mt_rand();   // 采用date函数生成命名规则 传入Y-m-d参数
            $info   =   $upload->uploadOne($_FILES['img']);
            if(!$info){
                $data['img']='';
            }else{
                $data['img']=  ltrim($info['savepath'].$info['savename'],'/');
            }
        }
        if(isset($_COOKIE["uid"])){
            $doctor->where("id=".$_COOKIE["uid"])->save($data);
            $log=D("Log");
            $opt['name']=$_SESSION['name'];
            $opt['action']='update';
            $opt['model']='Doctor';
            $opt['create_time']=time();
            $opt['log']='员工号为'.$_SESSION['name'].'对医生模块执行了修改操作,修改了id为'.$_COOKIE["uid"];
            $log->add($opt);
            $this->redirect("Doctor/lists");
        }
    }
    //修除数据
    public function del(){
        $id=I("request.id");
        $doctor=D("Doctor");
        $result=$doctor->where('id='.$id)->find();
        if($result['img']!=null){
            unlink("./imagecn_doc/".$result['img']);
        }
        $res=$doctor->where('id='.$id)->delete();
        if(!$res){
            echo json_encode(0);exit;
        }
        $log=D("Log");
        $opt['name']=$_SESSION['name'];
        $opt['action']='del';
        $opt['model']='Doctor';
        $opt['create_time']=time();
        $opt['log']='员工号为'.$_SESSION['name'].'对医生模块执行了删除操作,删除了id为'.$id;
        $log->add($opt);
        echo json_encode(1);exit;
    }

    public function auth(){
        $id=I('request.id');
        $type=I('request.type');
        $Doctor=D("Doctor");
        $auth_status=$Doctor->where('id='.$id)->getField('auth_status');
        if($auth_status!=0){ //已注册未认证的情况
            if($type==1){ //认证成功
                $map['id']=$id;
                $data['auth_status']=2;
                $data['update_time']=time();
                $status=$Doctor->where($map)->save($data);
                echo json_encode(1);exit;
            }


            if($type==2){ // 认证失败
                $map['id']=$id;
                $data['auth_status']=3;
                $data['update_time']=time();
                $status=$Doctor->where($map)->save($data);
                echo json_encode(1);exit;
            }
        }else{
             echo json_encode(0);exit;
        }
    }

}